#import pygame
#from pygame.locals import *
import os
import sys
import time

import net

class RawChatClient(net.TCPClient):
    defaultPort = 6317
    def __init__(self,port=defaultPort):
        self.sendingdata = []
        self.chat = []
        self.name = raw_input('What is your name: ')
        self.connect('99.110.58.77',port)
    def input(self):
        try:
            text = raw_input('Please enter text: ')
        except EOFError,KeyboardInterrupt:
            text = 'quit'
        if text == 'quit':
            self.quit()
            return False
        elif text:
            message = "["+self.name+":] "+text
            self.sendingdata = ["update","add message",message]
        else:
            self.sendingdata = []
        return True
    def update(self):
        data = self.check_for_data()
        if data:
            self.chat = data
            if self.sendingdata == []:
                self.sendingdata = ["update",None]
            self.send_data(self.sendingdata)
            self.sendingdata = []
    def draw(self):
        for line in self.chat:
            print line
    def run(self):
        self.send_data(["update","add self",self.name])
        self.chat = self.wait_for_data()
        self.send_data(["update",None])
        while True:
            if not self.input():
                break
            time.sleep(0.05)
            self.update()
            self.draw()


if __name__=='__main__':
    client = RawChatClient()
    client.run()
